package xin.config.web;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import xin.core.jwt.JwtAuthenticationEntryPoint;
import xin.core.jwt.JwtAuthenticationTokenFilter;
import xin.core.jwt.MyBCryptPasswordEncoder;

/**
 * <pre>
 * Web Security Config
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/7/28 17:14
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Slf4j
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.userDetailsService)
                .passwordEncoder(passwordEncoder());

    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new MyBCryptPasswordEncoder();
    }

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
        return new JwtAuthenticationTokenFilter();
    }

    @Bean
    public CorsFilter corsFilter() throws Exception {
        return new CorsFilter();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // 不使用CSRF
                .csrf().disable()
                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                // 不创建session
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                // 允许静态资源请求
                .antMatchers(
                        "/**",
                        "/**/**",
                        "/",
//                        "/*.html",
//                        "/*.ico",
//                        "/*.gif",
//                        "/*.jpg",
//                        "/*.bmp",
//                        "/*.png",
//                        "/*.css",
//                        "/*.js",
//                        "/**/*.html",
//                        "/**/*.ico",
//                        "/**/*.gif",
//                        "/**/*.jpg",
//                        "/**/*.bmp",
//                        "/**/*.png",
//                        "/**/*.css",
//                        "/**/*.js",
//                        "/api/**",
                        "/swagger-ui.html",
                        "/swagger-resources/**",
                        "/webjars/**"
//                        "/v2/*",
//                        "/login",
//                        "/register"
                ).permitAll();
//                .anyRequest().authenticated();


        /**
         * CROS Filter
         */
        httpSecurity.addFilterBefore(corsFilter(), UsernamePasswordAuthenticationFilter.class);
        /**
         * Custom JWT based security filter
         */
        httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

        /**
         * disable page caching
         */
        httpSecurity.headers().cacheControl();
    }

    @Bean
    public LoginSuccessHandler loginSuccessHandler(){
        return new LoginSuccessHandler();
    }
}
